package rickyGame.controller.state
{
	import flash.geom.Point;
	
	import ricky.game.Actor;
	import ricky.utils.TimeServer;
	import ricky.utils.Utils;
	
	import rickyGame.controller.Controller;
	import rickyGame.game.Fighter;
	import rickyGame.game.GameState;
	import rickyGame.game.Player;
	import rickyGame.game.action.ArriveAttackRangeAct;
	import rickyGame.game.action.MsgAct;
	import rickyGame.game.action.TimeAnimAct;
	import rickyGame.game.skill.SkillData;
	import rickyGame.game.skill.SkillEffectTargetType;
	import rickyGame.net.MsgType;
	import rickyGame.net.WorkQueue;
	import rickyGame.ui.Cursor;
	import rickyGame.ui.UIManager;
	
	/**
	 * 技能攻击
	 */
	public class ManaState extends State
	{
		public function ManaState(par:*=null)
		{
			super(par);
		}
		
		override public function create():void
		{
			Cursor.show(Cursor.MAGIC);
		}
		
		override public function operate(par:*):void
		{
			if(par is Array)
			{
				UIManager.inst.uiLayer.graphics.clear();
				var me:Player=Controller.instance.actor;
				var cSkill:SkillData=me.skills.currSkill;
				var point:Point;//攻击点
				var target:Fighter;//攻击目标
				
				point=par[0];
				
				if(cSkill) //技能攻击
				{
					//判断距离类型判断
					var rangeMax:int=cSkill.maxCastSkillDistance;
					var rangeMin:int=cSkill.miniCastSkillDistance;
					var cdnow:Number=new Date().getTime();
					var dt:Number=(cdnow-cSkill.lastUseTime);
					var v:Number=me.fightPro.mp.agileValue;
					
					if(cSkill.cast>v) //魔法值不够
					{
//						WorkQueue.instance.pushRecive(MsgType.CLIENT_SHOW_TIPS,CLocale.getValue("needMana"));
						return;
					}
					if(cSkill.cooldown>dt)	//判断cd时间
					{
//						WorkQueue.instance.pushRecive(MsgType.CLIENT_SHOW_TIPS,CLocale.getValue("needcooldown"));
						return;
					}
					
					if(par.length>1)
					{
						target=par[1];
					}
					
					//攻击目标判断
					if(cSkill.spellTarget.indexOf(SkillEffectTargetType.unit)>=0)//如果目标是单位
					{
						if(!target)
						{
//							WorkQueue.instance.pushRecive(MsgType.CLIENT_SHOW_TIPS,CLocale.getValue("errorTarget"));
							return;
						}
					}
					
					if(cSkill.spellTarget.indexOf(SkillEffectTargetType.self)>=0) //如果影响目标是自己
					{
						target=me;
					}
					
					if(cSkill.spellTarget.indexOf(SkillEffectTargetType.enemy)>=0)
					{
						if(target.group==me.group)
						{
//							WorkQueue.instance.pushRecive(MsgType.CLIENT_SHOW_TIPS,CLocale.getValue("enemyOnly"));
							return;
						}
					}
					
					if(cSkill.spellTarget.indexOf(SkillEffectTargetType.friends)>=0)
					{
						if(target.group!=me.group)
						{
//							WorkQueue.instance.pushRecive(MsgType.CLIENT_SHOW_TIPS,CLocale.getValue("friendsOnly"));
							return;
						}
					}
					
					
					var l:int=Utils.gridDistance1(me.graphic.x,me.graphic.y,point.x,point.y);
					var ani:Array,end:Number,actw:TimeAnimAct,msg:MsgAct;
					
					if(cSkill.waitTime>0) //如果等待时间大于0
					{
						ani=cSkill.actAnim.split(",");
						end=TimeServer.now()+cSkill.continueTime;
						actw=new TimeAnimAct(me,ani[0],end);
						msg=new MsgAct(me,MsgType.SERVER_ABILITY_ATTACKTARGET,me,cSkill.abilityID,point,target);
						actw.addNext(msg);
					}
					
					if(l<rangeMin || l>rangeMax) //如果不在攻击范围
					{
						var act:ArriveAttackRangeAct=new ArriveAttackRangeAct(me,target,point);
						if(actw)
							act.addNext(actw);
						else
						{
							msg=new MsgAct(me,MsgType.SERVER_ABILITY_ATTACKTARGET,me,cSkill.abilityID,point,target);
							act.addNext(msg);
						}
						me.addAct(act);
//						return;
					}
//					else
//						WorkQueue.instance.pushRecive(MsgType.SERVER_ABILITY_ATTACKTARGET,me,cSkill.abilityID,point,target);//同步目标对象
					
					me.skills.currSkill=null;
				}
			}
			Controller.instance.switchSate(new StopSate());
		}
	}
}